import pymysql
import datetime
from hashlib import sha1
from settings import dbParmas
dbParmas['db'] = 'bbs'
conn = pymysql.Connect(**dbParmas)
cursor = conn.cursor()
try:
    username = input('请输入您的注册用户名')
    password = input('请输入您的密码')
    email = input('请输入您的邮箱')
    if len(username)<=2 or username.isspace():
        print("用户名格式不正确")
        exit()
    result = cursor.execute("select * from user where username='{}'".format(username))
    if result > 0:
        print('重新输入')
        exit()
    sql = "insert into user(username,password,regtime,email) values('{}','{}','{}','{}')".format(username,sha1(password.encode('utf8')).hexdigest(),datetime.datetime.now(),email)
    cursor.execute(sql)
    conn.commit()
except Exception as e:
    print(e)
finally:
    cursor.close()
    conn.close()
